package com.wzdigit.wms.wms.controller.basic;

import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.framework.model.response.ErrorEntity;
import com.wzdigit.wms.common.utils.ValidatorUtil;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.request.basic.CreateZoneRequest;
import com.wzdigit.wms.basic.client.request.basic.SearchZoneRequest;
import com.wzdigit.wms.basic.client.request.basic.UpdateStatusZoneRequest;
import com.wzdigit.wms.basic.client.request.basic.UpdateZoneRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.basic.ZoneApiResponse;
import com.wzdigit.wms.basic.client.response.basic.ZoneResponse;
import com.wzdigit.wms.wms.constant.WmsWareConstant;
import com.wzdigit.wms.wms.service.basic.ZoneService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 库区 前端控制器
 * @author yangdongjie
 * @ZoneController.java
 * @2020年12月31日下午5:56:18
 */
@RestController
@Api(tags = "库区")
@RequestMapping(value = WmsWareConstant.API_VERSION)
public class ZoneController {

    @Autowired
    private ZoneService zoneService;

    private static final String REQUEST_MODEL = "/zone";

    
	/**
	 * 库区（下拉框）列表（启用状态）
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 */
	@ApiOperation(value = "库区（下拉框）列表（启用状态）",notes = "库区（下拉框）列表（启用状态）")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/list")
    public List<ZoneApiResponse> list(
    		@ApiParam(value = "仓库ID", required = false) 
    		@RequestParam(required = false) Integer whsId,
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return zoneService.list(whsId);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    /**
     * 级联查询库区（下拉框）列表（所有状态）
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "级联查询库区（下拉框）列表（所有状态）",notes = "级联查询库区（下拉框）列表（所有状态）")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/listAllByzoneGroup")
    public List<ZoneApiResponse> listAllByzoneGroup(
            @ApiParam(value = "区域Id", required = false)
            @RequestParam(required = false) Integer zoneGroupId,
            HttpServletRequest servletRequest,
            HttpServletResponse servletResponse) {
        try {
            return zoneService.listAllZoneGroupsIds(zoneGroupId);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
	
    /**
     * 查询库区列表
     * @param dto
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @SuppressWarnings("rawtypes")
	@ApiOperation(value = "查询库区列表",notes = "查询库区列表")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/searchZone")
    public PagerInfo<List<ZoneResponse>> searchZone(
    		SearchZoneRequest dto, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return zoneService.searchZone(dto);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    
    /**
     * 查询库区
     * @param zoneId
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "查询库区",notes = "查询库区")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/getZone")
    public ZoneResponse getZone(
    		@ApiParam(value = "库区ID", required = true) 
    		@RequestParam Integer zoneId, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return zoneService.getZone(zoneId);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    
    /**
     * 创建库区
     * @param dto
     * @param bindingResult
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "创建库区",notes = "创建库区")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PostMapping(value = REQUEST_MODEL + "/createZone")
    public ZoneResponse createZone(
    		@RequestBody @Valid CreateZoneRequest dto, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
        	ValidatorUtil.validator(bindingResult);
            return zoneService.createZone(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    /**
     * 修改库区
     * @param dto
     * @param bindingResult
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "修改库区",notes = "修改库区")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PutMapping(value = REQUEST_MODEL + "/updateZone")
    public ZoneResponse updateZone(
    		@RequestBody @Valid UpdateZoneRequest dto, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
        	ValidatorUtil.validator(bindingResult);
            return zoneService.updateZone(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    /**
     * 修改库区状态
     * @param dto
     * @param bindingResult
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "修改库区状态",notes = "修改库区状态")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PutMapping(value = REQUEST_MODEL + "/updateStatus")
    public <T> Result<T> updateStatus(
    		@RequestBody @Valid UpdateStatusZoneRequest dto, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
        	ValidatorUtil.validator(bindingResult);
            return zoneService.updateStatus(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
}
